<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Java Native Access</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Java_Native_Access"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Java_Native_Access rootpage-Java_Native_Access skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Java Native Access</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><style data-mw-deduplicate="TemplateStyles:r1295905060">
/* start https://en.wikipedia.org/ */
.mw-parser-output .infobox-subbox{padding:0;border:none;margin:-3px;width:auto;min-width:100%;font-size:100%;clear:none;float:none;background-color:transparent}.mw-parser-output .infobox-3cols-child{margin:auto}.mw-parser-output .infobox .navbar{font-size:100%}@media screen{html.skin-theme-clientpref-night .mw-parser-output .infobox-full-data:not(.notheme)>div:not(.notheme)[style]{background:#1f1f23!important;color:#f8f9fa}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .infobox-full-data:not(.notheme)>div:not(.notheme)[style]{background:#1f1f23!important;color:#f8f9fa}}@media(min-width:640px){body.skin--responsive .mw-parser-output .infobox-table{display:table!important}body.skin--responsive .mw-parser-output .infobox-table>caption{display:table-caption!important}body.skin--responsive .mw-parser-output .infobox-table>tbody{display:table-row-group}body.skin--responsive .mw-parser-output .infobox-table th,body.skin--responsive .mw-parser-output .infobox-table td{padding-left:inherit;padding-right:inherit}}
/* end https://en.wikipedia.org/ */
</style><table class="infobox vevent"><tbody><tr><th colspan="2" class="infobox-above summary">Java Native Access</th></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="Programmer" title="Programmer">Original author(s)</a></th><td class="infobox-data">Todd Fast, Timothy Wall, Liang Chen</td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;">Initial release</th><td class="infobox-data">May 9, 2007<span style="display: none;"> (<span class="bday dtstart published updated itvstart">2007-05-09</span>)</span></td></tr><tr style="display: none;"><td colspan="2" class="infobox-full-data"></td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="Software_release_life_cycle" title="Software release life cycle">Stable release</a></th><td class="infobox-data"><div style="margin:0px;">5.17.0
/ March 16, 2025<span style="display:none"> (<span class="bday dtstart published updated">2025-03-16</span>)</span><sup id="cite_ref-5.17.0_1-0" class="reference"><a href="#cite_note-5.17.0-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup></div></td></tr><tr style="display:none"><td colspan="2">
</td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="Repository_(version_control)" title="Repository (version control)">Repository</a></th><td class="infobox-data"><style data-mw-deduplicate="TemplateStyles:r1126788409">
/* start https://en.wikipedia.org/ */
.mw-parser-output .plainlist ol,.mw-parser-output .plainlist ul{line-height:inherit;list-style:none;margin:0;padding:0}.mw-parser-output .plainlist ol li,.mw-parser-output .plainlist ul li{margin-bottom:0}
/* end https://en.wikipedia.org/ */
</style><div class="plainlist"><ul><li><span class="url"><a rel="nofollow" class="external text" href="https://github.com/java-native-access/jna">github<wbr>.com<wbr>/java-native-access<wbr>/jna</a></span> </li></ul>
</div></td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;">Written in</th><td class="infobox-data"><a href="C_(programming_language)" title="C (programming language)">C</a> and <a href="Java_(programming_language)" title="Java (programming language)">Java</a></td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="Operating_system" title="Operating system">Operating system</a></th><td class="infobox-data">Windows, macOS, Android, AIX, FreeBSD, <a href="Linux" title="Linux">Linux</a>, OpenBSD, Solaris, Windows Mobile</td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="Computing_platform" title="Computing platform">Platform</a></th><td class="infobox-data"><a href="Java_(software_platform)" title="Java (software platform)">Java</a> 1.4 or later (for JNA 3.5.2 or earlier), Java 1.6 for JNA 4.0.0 and later</td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="File_size" title="File size">Size</a></th><td class="infobox-data">1.83 MB (archived)</td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="Software_categories#Categorization_approaches" title="Software categories">Type</a></th><td class="infobox-data"><a href="Library_(computer_science)" class="mw-redirect" title="Library (computer science)">Software Library</a></td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;"><a href="Software_license" title="Software license">License</a></th><td class="infobox-data"><a href="LGPL" class="mw-redirect" title="LGPL">LGPL</a> version 2.1 or later and (from version 4.0 onward) the Apache Software License, version 2.0</td></tr><tr><th scope="row" class="infobox-label" style="white-space: nowrap;">Website</th><td class="infobox-data"><span class="url"><a rel="nofollow" class="external text" href="https://github.com/java-native-access/jna">github<wbr>.com<wbr>/java-native-access<wbr>/jna</a></span></td></tr></tbody></table>
<p><b>Java Native Access</b> (<b>JNA</b>) is a community-developed library that provides <a href="Java_(software_platform)" title="Java (software platform)">Java</a> programs easy access to <a href="Shared_library" title="Shared library">native shared libraries</a> without using the <a href="Java_Native_Interface" title="Java Native Interface">Java Native Interface</a> (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no <a href="Boilerplate_code" title="Boilerplate code">boilerplate</a> or generated <a href="Glue_code" title="Glue code">glue code</a> is required.
</p><p>Since Java 22, the <a href="Java_Native_Interface#Foreign_Function_and_Memory_API" title="Java Native Interface">Foreign Function and Memory API</a> was provided as a standard modern alternative.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Architecture">Architecture</h2></div>
<p>The JNA library uses a small native library called <a href="Foreign_function_interface" title="Foreign function interface">foreign function interface</a> library (<a href="Libffi" title="Libffi">libffi</a>) to dynamically invoke <a href="Native_code" class="mw-redirect" title="Native code">native code</a>. The JNA library uses native functions allowing code to load a library by name and retrieve a <a href="Pointer_(computer_programming)" title="Pointer (computer programming)">pointer</a> to a function within that library, and uses <a href="Libffi" title="Libffi">libffi</a> library to invoke it, all without <a href="Static_binding" class="mw-redirect" title="Static binding">static bindings</a>, <a href="Header_files" class="mw-redirect" title="Header files">header files</a>, or any compile phase. The developer uses a <a href="Java_interface" class="mw-redirect" title="Java interface">Java interface</a> to describe functions and structures in the target native library. This makes it quite easy to take advantage of native platform features without incurring the high development overhead of configuring and building <a href="JNI" class="mw-redirect" title="JNI">JNI</a> code.
</p><p>JNA is built and tested on <a href="MacOS" title="MacOS">macOS</a>, <a href="Microsoft_Windows" title="Microsoft Windows">Microsoft Windows</a>, <a href="FreeBSD" title="FreeBSD">FreeBSD</a> / <a href="OpenBSD" title="OpenBSD">OpenBSD</a>, <a href="Solaris_(operating_system)" class="mw-redirect" title="Solaris (operating system)">Solaris</a>, <a href="Linux" title="Linux">Linux</a>, <a href="AIX" class="mw-redirect" title="AIX">AIX</a>, <a href="Windows_Mobile" title="Windows Mobile">Windows Mobile</a>, and <a href="Android_(operating_system)" title="Android (operating system)">Android</a>. It is also possible to tweak and recompile the native build configurations to make it work on most other platforms that run Java.
</p>
<div class="mw-heading mw-heading2"><h2 id="Mapping_types">Mapping types</h2></div>
<p>The following table shows an overview of types mapping between Java and native code and supported by the JNA library.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<table class="wikitable">
<tbody><tr>
<th>Native Type
</th>
<th>Size
</th>
<th>Java Type
</th>
<th>Common Windows Types
</th></tr>
<tr>
<td><a href="Character_(computing)" title="Character (computing)"><style data-mw-deduplicate="TemplateStyles:r886049734">
/* start https://en.wikipedia.org/ */
'"`UNIQ--templatestyles-00000008-QINU`"'
/* end https://en.wikipedia.org/ */
</style><span class="monospaced">char</span></a>
</td>
<td>8-bit integer
</td>
<td><span class="monospaced"><a href="Byte" title="Byte">byte</a></span>
</td>
<td><span class="monospaced">BYTE, TCHAR</span>
</td></tr>
<tr>
<td><a href="Short_integer" class="mw-redirect" title="Short integer"><span class="monospaced">short</span></a>
</td>
<td>16-bit integer
</td>
<td><a href="Short_integer" class="mw-redirect" title="Short integer"><span class="monospaced">short</span></a>
</td>
<td><span class="monospaced">WORD</span>
</td></tr>
<tr>
<td><a href="Wide_character" title="Wide character"><span class="monospaced">wchar_t</span></a>
</td>
<td>16/32-bit character
</td>
<td><a href="Character_(computing)" title="Character (computing)"><span class="monospaced">char</span></a>
</td>
<td><span class="monospaced">TCHAR</span>
</td></tr>
<tr>
<td><a href="Integer_(computer_science)" title="Integer (computer science)"><span class="monospaced">int</span></a>
</td>
<td>32-bit integer
</td>
<td><a href="Integer_(computer_science)" title="Integer (computer science)"><span class="monospaced">int</span></a>
</td>
<td><span class="monospaced">DWORD</span>
</td></tr>
<tr>
<td><a href="Integer_(computer_science)" title="Integer (computer science)"><span class="monospaced">int</span></a>
</td>
<td>boolean value
</td>
<td><span class="monospaced">boolean</span>
</td>
<td><span class="monospaced">BOOL</span>
</td></tr>
<tr>
<td><a href="Long_integer" class="mw-redirect" title="Long integer"><span class="monospaced">long</span></a>
</td>
<td>32/64-bit integer
</td>
<td><abbr title="com.sun.jna.NativeLong">NativeLong</abbr>
</td>
<td><span class="monospaced">LONG</span>
</td></tr>
<tr>
<td><a href="Long_long" class="mw-redirect" title="Long long">long long</a>
</td>
<td>64-bit integer
</td>
<td><a href="Long_integer" class="mw-redirect" title="Long integer"><span class="monospaced">long</span></a>
</td>
<td><span class="monospaced">__int64</span>
</td></tr>
<tr>
<td><a href="Single_precision" class="mw-redirect" title="Single precision"><span class="monospaced">float</span></a>
</td>
<td>32-bit FP
</td>
<td><a href="Single_precision" class="mw-redirect" title="Single precision"><span class="monospaced">float</span></a>
</td>
<td>
</td></tr>
<tr>
<td><a href="Double_precision" class="mw-redirect" title="Double precision"><span class="monospaced">double</span></a>
</td>
<td>64-bit FP
</td>
<td><a href="Double_precision" class="mw-redirect" title="Double precision"><span class="monospaced">double</span></a>
</td>
<td>
</td></tr>
<tr>
<td><span class="monospaced"><a href="Character_(computing)#char" title="Character (computing)">char</a>*</span>
</td>
<td>C string
</td>
<td><span class="monospaced">String</span>
</td>
<td><span class="monospaced">LPCSTR</span>
</td></tr>
<tr>
<td><span class="monospaced">void*</span>
</td>
<td><span class="monospaced">pointer</span>
</td>
<td><abbr title="com.sun.jna.Pointer">Pointer</abbr>
</td>
<td><span class="monospaced">LPVOID, HANDLE, LPXXX</span>
</td></tr></tbody></table>
<p><b>Note:</b> The meaning of <span class="monospaced">TCHAR</span> changes between <span class="monospaced">char</span> and <span class="monospaced">wchar_t</span> according to some preprocessor definitions. <span class="monospaced">LPCTSTR</span> follows.
</p>
<div class="mw-heading mw-heading2"><h2 id="Memory_byte_alignment_for_data_structures">Memory byte alignment for data structures</h2></div>
<p>Native libraries have no standardized memory byte alignment flavor. JNA defaults to an OS platform specific setting, that can be overridden by a library specific custom alignment. If the alignment details are not given in the documentation of the native library, the correct alignment must be determined by trial and error during implementation of the Java wrapper.
</p>
<div class="mw-heading mw-heading2"><h2 id="Example">Example</h2></div>
<p>The following program loads the local <a href="C_standard_library" title="C standard library">C standard library</a> implementation and uses it to call the <i><a href="Printf" title="Printf">printf</a></i> function.
</p><p><b>Note:</b> The following code is portable and works the same on <a href="Microsoft_Windows" title="Microsoft Windows">Windows</a> and POSIX (<a href="Linux" title="Linux">Linux</a> / <a href="Unix" title="Unix">Unix</a> / <a href="MacOS" title="MacOS">macOS</a>) platforms.
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kn">import</span><span class="w"> </span><span class="nn">com.sun.jna.Library</span><span class="p">;</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">com.sun.jna.Native</span><span class="p">;</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">com.sun.jna.Platform</span><span class="p">;</span>
<span class="cm">/** Simple example of native library declaration and usage. */</span>
<span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">HelloWorld</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">interface</span> <span class="nc">CLibrary</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Library</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">CLibrary</span><span class="w"> </span><span class="n">INSTANCE</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">CLibrary</span><span class="p">)</span><span class="w"> </span><span class="n">Native</span><span class="p">.</span><span class="na">loadLibrary</span><span class="p">(</span>
<span class="w"> </span><span class="p">(</span><span class="n">Platform</span><span class="p">.</span><span class="na">isWindows</span><span class="p">()</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">"msvcrt"</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="s">"c"</span><span class="p">),</span><span class="w"> </span><span class="n">CLibrary</span><span class="p">.</span><span class="na">class</span><span class="p">);</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">printf</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">format</span><span class="p">,</span><span class="w"> </span><span class="n">Object</span><span class="p">...</span><span class="w"> </span><span class="n">args</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">CLibrary</span><span class="p">.</span><span class="na">INSTANCE</span><span class="p">.</span><span class="na">printf</span><span class="p">(</span><span class="s">"Hello, World\n"</span><span class="p">);</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">args</span><span class="p">.</span><span class="na">length</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">CLibrary</span><span class="p">.</span><span class="na">INSTANCE</span><span class="p">.</span><span class="na">printf</span><span class="p">(</span><span class="s">"Argument %d: %s\n"</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">args</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>The following program loads the <a href="C_POSIX_library" title="C POSIX library">C POSIX library</a> and uses it to call the standard <i><a href="Mkdir" title="Mkdir">mkdir</a></i> function.
</p><p><b>Note:</b> The following code is portable and works the same on <a href="POSIX" title="POSIX">POSIX</a> standards platforms.
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kn">import</span><span class="w"> </span><span class="nn">com.sun.jna.Library</span><span class="p">;</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">com.sun.jna.Native</span><span class="p">;</span>
<span class="cm">/** Simple example of native C POSIX library declaration and usage. */</span>
<span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">ExampleOfPOSIX</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">interface</span> <span class="nc">POSIX</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Library</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">chmod</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mode</span><span class="p">);</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">chown</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">user</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">group</span><span class="p">);</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">rename</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">oldpath</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">newpath</span><span class="p">);</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">kill</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">pid</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">signal</span><span class="p">);</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">link</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">oldpath</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">newpath</span><span class="p">);</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">mkdir</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">path</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">mode</span><span class="p">);</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">rmdir</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">path</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// It is possible to load msvcrt for its partial POSIX support on Windows...</span>
<span class="w"> </span><span class="n">POSIX</span><span class="w"> </span><span class="n">posix</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">POSIX</span><span class="p">)</span><span class="w"> </span><span class="n">Native</span><span class="p">.</span><span class="na">loadLibrary</span><span class="p">(</span><span class="s">"c"</span><span class="p">,</span><span class="w"> </span><span class="n">POSIX</span><span class="p">.</span><span class="na">class</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// but it will still fail on Windows due to /tmp being missing.</span>
<span class="w"> </span><span class="n">posix</span><span class="p">.</span><span class="na">mkdir</span><span class="p">(</span><span class="s">"/tmp/newdir"</span><span class="p">,</span><span class="w"> </span><span class="mo">0777</span><span class="p">);</span>
<span class="w"> </span><span class="n">posix</span><span class="p">.</span><span class="na">rename</span><span class="p">(</span><span class="s">"/tmp/newdir"</span><span class="p">,</span><span class="s">"/tmp/renamedir"</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>The program below loads the <b><a href="Kernel32.dll" class="mw-redirect" title="Kernel32.dll">Kernel32.dll</a></b> and uses it to call the <i><a href="Beep_(sound)" title="Beep (sound)">Beep</a></i> and <i><a href="Sleep_(system_call)" title="Sleep (system call)">Sleep</a></i> functions.
</p><p><b>Note:</b> The following code works only on <a href="Microsoft_Windows" title="Microsoft Windows">Windows</a> platforms.
</p>
<div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span class="kn">import</span><span class="w"> </span><span class="nn">com.sun.jna.Library</span><span class="p">;</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">com.sun.jna.Native</span><span class="p">;</span>
<span class="cm">/** Simple example of Windows native library declaration and usage. */</span>
<span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">BeepExample</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">interface</span> <span class="nc">Kernel32</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Library</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// FREQUENCY is expressed in hertz and ranges from 37 to 32767</span>
<span class="w"> </span><span class="c1">// DURATION is expressed in milliseconds</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">boolean</span><span class="w"> </span><span class="nf">Beep</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">FREQUENCY</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">DURATION</span><span class="p">);</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">Sleep</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">DURATION</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Kernel32</span><span class="w"> </span><span class="n">lib</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Kernel32</span><span class="p">)</span><span class="w"> </span><span class="n">Native</span><span class="p">.</span><span class="na">loadLibrary</span><span class="p">(</span><span class="s">"kernel32"</span><span class="p">,</span><span class="w"> </span><span class="n">Kernel32</span><span class="p">.</span><span class="na">class</span><span class="p">);</span>
<span class="w"> </span><span class="n">lib</span><span class="p">.</span><span class="na">Beep</span><span class="p">(</span><span class="mi">698</span><span class="p">,</span><span class="w"> </span><span class="mi">500</span><span class="p">);</span>
<span class="w"> </span><span class="n">lib</span><span class="p">.</span><span class="na">Sleep</span><span class="p">(</span><span class="mi">500</span><span class="p">);</span>
<span class="w"> </span><span class="n">lib</span><span class="p">.</span><span class="na">Beep</span><span class="p">(</span><span class="mi">698</span><span class="p">,</span><span class="w"> </span><span class="mi">500</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1266661725">
/* start https://en.wikipedia.org/ */
.mw-parser-output .portalbox{padding:0;margin:0.5em 0;display:table;box-sizing:border-box;max-width:175px;list-style:none}.mw-parser-output .portalborder{border:1px solid var(--border-color-base,#a2a9b1);padding:0.1em;background:var(--background-color-neutral-subtle,#f8f9fa)}.mw-parser-output .portalbox-entry{display:table-row;font-size:85%;line-height:110%;height:1.9em;font-style:italic;font-weight:bold}.mw-parser-output .portalbox-image{display:table-cell;padding:0.2em;vertical-align:middle;text-align:center}.mw-parser-output .portalbox-link{display:table-cell;padding:0.2em 0.2em 0.2em 0.3em;vertical-align:middle}@media(min-width:720px){.mw-parser-output .portalleft{margin:0.5em 1em 0.5em 0}.mw-parser-output .portalright{clear:right;float:right;margin:0.5em 0 0.5em 1em}}
/* end https://en.wikipedia.org/ */
</style>
<ul><li><a href="JNAerator" title="JNAerator">JNAerator</a></li>
<li><a href="Platform_Invocation_Services" title="Platform Invocation Services">P/Invoke</a></li>
<li><a href="SWIG" title="SWIG">SWIG</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-5.17.0-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-5.17.0_1-0">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://github.com/java-native-access/jna/releases/tag/5.17.0">"Release 5.17.0"</a>. <i><a href="GitHub" title="GitHub">GitHub</a></i>. 2025-03-16.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://github.com/twall/jna/blob/master/www/Mappings.md">"Default Type Mappings"</a>. jna.dev.java.net<span class="reference-accessdate">. Retrieved <span class="nowrap">2011-08-02</span></span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="https://github.com/java-native-access/jna#readme">Java Native Access Web Page</a></li>
<li><a rel="nofollow" class="external text" href="https://github.com/java-native-access/jna#download">Java Native Access - Download page</a></li>
<li><a rel="nofollow" class="external text" href="https://groups.google.com/group/jna-users/about">Java Native Access - User Mailing List</a></li>
<li><cite id="CITEREFFriesen2008" class="citation web cs1">Friesen, Jeff (5 February 2008). <a rel="nofollow" class="external text" href="https://www.infoworld.com/article/2077828/open-source-java-projects-java-native-access.html">"Open source Java projects: Java Native Access"</a>. Open Source Java Tutorials. <i><a href="JavaWorld" class="mw-redirect" title="JavaWorld">JavaWorld</a></i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-07-27</span></span>.</cite></li>
<li><cite id="CITEREFMorris2009" class="citation web cs1">Morris, Stephen B. (20 May 2009). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20150113014701/https://today.java.net/pub/a/today/2009/05/19/protect-your-legacy-code-jna.html">"Protect Your Legacy Code Investment with JNA"</a>. <i>today.java.net</i>. Archived from <a rel="nofollow" class="external text" href="https://today.java.net/pub/a/today/2009/05/19/protect-your-legacy-code-jna.html">the original</a> on 2015-01-13.</cite></li>
<li><cite id="CITEREFDasgupta2009" class="citation web cs1">Dasgupta, Sanjay (11 November 2009). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20091115074230/http://today.java.net/article/2009/11/11/simplify-native-code-access-jna">"Simplify Native Code Access with JNA"</a>. <i>today.java.net</i>. Archived from <a rel="nofollow" class="external text" href="http://today.java.net/article/2009/11/11/simplify-native-code-access-jna">the original</a> on 2009-11-15.</cite></li>
<li><cite id="CITEREFDoubrovkine2011" class="citation web cs1">Doubrovkine, Daniel (20 June 2011). <a rel="nofollow" class="external text" href="https://code.dblock.org/2011/06/20/jna-is-now-a-githubber.html">"JNA is now a Githubber"</a>. <i>code.dblock.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-07-27</span></span>.</cite></li>
<li><cite id="CITEREFKiaer2010" class="citation web cs1">Kiaer, Jesper (21 March 2010). <a rel="nofollow" class="external text" href="https://nevermind.dk/nevermind/blog.nsf/subject/calling-the-domino-c-api-from-an-xpage-or-a-java-agent">"Calling the Lotus Domino C-API with JNA"</a>. <i>Nevermind.dk</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-07-27</span></span>.</cite></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-30" href="https://en.wikipedia.org/wiki/?title=Java_Native_Access&oldid=1303398719">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>